#include "Mystring.h"
#include <cstring>
mystring::mystring():size(0),capacity(15){
    s=new char[capacity];
}

mystring::mystring(const char *str){
    size=strlen(str);
    capacity=size+1>15?size+1:15;
    s=new char[capacity];
    strcpy(s,str);
}
mystring::mystring(const mystring& str){
    size=str.size;
    capacity=str.capacity;
    s=new char[capacity];
    strcpy(s,str.s);
}
mystring& mystring::operator=(const mystring &str){
    delete []s;
    size=str.size;
    capacity=str.capacity;
    s=new char[capacity];
    strcpy(s,str.s);
    return *this;
}
mystring::~mystring()
{
    delete []s;
}

mystring& mystring::operator+=(const mystring &string){
    size+=string.size;
    if(size+1>capacity)
        reSize(size+1);
    strcat(this->s,string.s);
    return *this;
}

mystring& mystring::operator+=(const char &c){
    if(size+1>capacity)
        reSize(size*2);
    this->s[size++]=c;
    this->s[size]='\0';
    return *this;
}

mystring mystring::operator+(const mystring &string)
{
    mystring result =*this;
    result+=string;
    return result;
}

mystring &mystring::operator-=(const mystring &string)
{
    
    char *dest=strstr(s,string.s);
    while(dest!=nullptr){                    
        char *back=dest+string.size;
        while(*back!='\0'){        
            *dest++=*back++;
        }
        *dest='\0';
        this->size-=string.size;
        dest=strstr(s,string.s);
    }
    if(size+1<capacity/2)
        reSize(capacity/2);
    return *this;
}

mystring mystring::operator-(const mystring &string)
{
    mystring result =*this;
    result-=string;
    return result;
}

char &mystring::operator[](int index)
{
    return this->s[index];
}

bool mystring::operator==(const mystring &string)
{
    return strcmp(s,string.s)==0;
}

bool mystring::operator!=(const mystring &string)
{
    return strcmp(s,string.s)!=0;
}

mystring::operator int()
{
    return atoi(this->s);
}

mystring::operator double()
{
    return atof(this->s);
}

mystringArray mystring::split(const mystring &delim)
{
    mystring temp =*this;
    mystringArray result;
    char *dest =strtok(temp.s,delim.s);
    while(dest!=nullptr){
        result+=dest;
        dest=strtok(nullptr,delim.s);
    }
    return result;      
}

// bool mystring::operator&&(const mystring &string)
// {
//     return this->size!=0 && string.size!=0;
// }

void mystring::reSize(int capacity){
    this->capacity=capacity;
    char *newS=new char[capacity];
    size= size>capacity-1?capacity-1:size;
    strncpy(newS,s,size);
    delete []s;
    s=newS;
}

std::ostream& operator<<(std::ostream &os,const mystring& str)
{
    os<<str.s;
    return os;
}

std::istream &operator>>(std::istream &is, mystring &str)
{

    char c=0;
    //读到回车结束
    while((c=getchar())!='\n'&&c!=' '){
        str+=c;
    }
    return is;
}



mystringArray::mystringArray():size(0),capacity(15){
    array=new mystring[capacity];
}
mystringArray::mystringArray(const mystringArray& a){
    size=a.size;
    capacity=a.capacity;
    array=new mystring[capacity];
    for(int i=0;i<size;i++){
        array[i]=a.array[i];
    }
}
mystringArray& mystringArray::operator=(const mystringArray& a){
    delete []array;
    size=a.size;
    capacity=a.capacity;
    array=new mystring[capacity];
    for(int i=0;i<size;i++){
        array[i]=a.array[i];
    }
    return *this;
}
mystringArray::~mystringArray(){
    delete []array;
}

mystring &mystringArray::operator[](int index)
{
   return this->array[index];
}

bool mystringArray::operator==(const mystringArray &a)
{
    if(this->size==a.size)
    {
        for(int i=0;i<size;i++)
        {
            if(this->array[i]==a.array[i])
                return true;
            else
                return false;
        }
    }
        return false;
}

mystringArray &mystringArray::operator+=(const mystring &str)
{
    if(size==capacity){
        capacity*=2;
        mystring *newS=new mystring[capacity];
        for(int i=0;i<size;i++){
        newS[i]=array[i];
        }
        delete []array;
        array=newS;
    }
        
    array[size++]=str;
    return *this;
}

mystringArray mystringArray::operator+(const mystring &str)
{
    mystringArray result=*this;
    result+=str;
    return result;
}

mystringArray &mystringArray::operator-=(const mystring &str)
{
   int index=0;
   while(index<size)
   {
        for(int i=index;i<size;i++)
        {
            if(array[index]==str)
            {
                array[i]=array[i++];
            }
            index--;
            size--;
        }
        index++;
   }
    if(size+1<capacity/2)
    {
        capacity/=2;
        mystring *newS=new mystring[capacity];
         for(int i=0;i<size;i++)
         {
            newS[i]=array[i];
         }
         delete []array;
         array=newS;
    }
        return *this;

}

mystringArray mystringArray::operator-(const mystring &str)
{
     mystringArray result=*this;
    result-=str;
    return result;
}


std::ostream &operator<<(std::ostream &os, const mystringArray &a)
{           
    for(int i=0;i<a.size;i++){
        os<<a.array[i]<<std::endl;
    }
    return os;
}

std::istream &operator>>(std::istream &is, mystringArray &a)
{
    is>>a.size;
    getchar();
    for(int i=0;i<a.size;i++)
    {
        is>>a.array[i];
    }
    return is;
}
